/******************************************************************************
*File   : IniFile.h
*Author : Lan Hu
*Email	: Hi_Blue@126.com
*Notes	:
*Licence: 
*Created: 2009-09-03
*******************************************************************************/

#ifndef _MYLIB_INIFILE_H_INCLUDE_20090903_
#define _MYLIB_INIFILE_H_INCLUDE_20090903_

#include "public.h"
#include "File.h"

M_BEGIN_NAMESPACE(mylib)

class IniFile
{
public:
	enum OPEN_MODE{E_MODE_READ,E_MODE_WRITE,E_MODE_DEFAULT};
	enum {E_MAX_LENGTH = 64};

private:
	File * m_File;

public:
	IniFile(char * szName = "config.ini")
	{
		m_File = new File(szName);
		assert(m_File);
	}

	IniFile(string name)
	{
		m_File = new File(name.c_str());
		assert(m_File);
	}

	bool open(OPEN_MODE mode = E_MODE_DEFAULT)
	{
		bool bReturn;
		switch (mode)
		{
		case E_MODE_READ:
			bReturn = m_File->open(File::E_MODE_READ);
			break;
		case E_MODE_WRITE:
			bReturn = m_File->open(File::E_MODE_WRITE);
			break;
		default:
			bReturn = m_File->open(File::E_MODE_READ | File::E_MODE_APPEND);
			break;
		}
		return bReturn;
	}

	int close()
	{
		return m_File->close();
	}

	bool write(char *szKey, int iValue)
	{
		assert(szKey);
		bool bSusscee = false;
		if (m_File != NULL)
		{
			int len = m_File->writeFormat("%s = %d\n",szKey,iValue);
			if (len>0)
			{
				bSusscee = true;
			}
		}
		return bSusscee;
	}

	bool write(string key, int iValue)
	{
		assert(key.length()>0);
		bool bSusscee = false;
		if (m_File != NULL)
		{
			int len = m_File->writeFormat("%s = %d\n",key.c_str(),iValue);
			if (len>0)
			{
				bSusscee = true;
			}
		}
		return bSusscee;
	}

	bool write(char *szKey, char *szValue)
	{
		assert(szKey);
		assert(szValue);
		bool bSusscee = false;
		if (m_File != NULL)
		{
			int len = m_File->writeFormat("%s = %s\n",szKey,szValue);
			if (len>0)
			{
				bSusscee = true;
			}
		}
		return bSusscee;
	}

	bool write(string key, string value)
	{
		assert(key.length());
		assert(value.length()>0);
		bool bSusscee = false;
		if (m_File != NULL)
		{
			int len = m_File->writeFormat("%s = %s\n",key.c_str(),value.c_str());
			if (len>0)
			{
				bSusscee = true;
			}
		}
		return bSusscee;
	}

	static bool Write(char *szKey, int iValue,char *szFileName)
	{
		assert(szKey);
		assert(szFileName);
		bool bSusscee = false;
		File::T_FILE * pFile;
		pFile = File::Open(szFileName, File::E_MODE_APPEND);
		if (pFile != NULL)
		{
			int len = File::WriteFormat(pFile,"%s = %d\n",szKey,iValue);
			if (len>0)
			{
				bSusscee = true;
			}
			File::Close(pFile);
		}
		return bSusscee;
	}

	static bool Write(string key, int iValue,string fileName)
	{
		assert(key.length()>0);
		assert(fileName.length()>0);
		bool bSusscee = false;
		File::T_FILE * pFile;
		pFile = File::Open(fileName.c_str(), File::E_MODE_APPEND);
		if (pFile != NULL)
		{
			int len = File::WriteFormat(pFile,"%s = %d\n",key.c_str(),iValue);
			if (len>0)
			{
				bSusscee = true;
			}
			File::Close(pFile);
		}
		return bSusscee;
	}

	static bool Write(char *szKey, char *szValue,char *szFileName)
	{
		assert(szKey);
		assert(szValue);
		assert(szFileName);
		bool bSusscee = false;
		File::T_FILE * pFile;
		pFile = File::Open(szFileName, File::E_MODE_APPEND);
		if (pFile != NULL)
		{
			int len = File::WriteFormat(pFile,"%s = %s\n",szKey,szValue);
			if (len>0)
			{
				bSusscee = true;
			}
			File::Close(pFile);
		}
		return bSusscee;
	}

	bool read(char *szKey, char *szValue)
	{
		assert(szKey);
		assert(szValue);
		bool bHaveFound = false;
		char szRead[E_MAX_LENGTH];
		char _szKey[E_MAX_LENGTH/2];
		const int iKEY_LEN = strlen(szKey);
		while(iKEY_LEN<E_MAX_LENGTH)
		{
			m_File->getString(szRead,E_MAX_LENGTH);
			if (strcmp(szRead,"") == 0)
			{
				break;
			}
			if (strncmp(szKey,szRead,iKEY_LEN) != 0)
			{
				continue;
			}
			sscanf(szRead,"%s = %s\n",_szKey,szValue);
			bHaveFound = true;
			break;
		}
		return bHaveFound;
	}

	bool read(char *szKey, int *iValue)
	{
		assert(szKey);
		assert(iValue);
		bool bHaveFound = false;
		char szRead[E_MAX_LENGTH];
		char _szKey[E_MAX_LENGTH/2];
		const int iKEY_LEN = strlen(szKey);
		while(iKEY_LEN<E_MAX_LENGTH)
		{
			m_File->getString(szRead,E_MAX_LENGTH);
			if (strcmp(szRead,"") == 0)
			{
				break;
			}
			if (strncmp(szKey,szRead,iKEY_LEN) != 0)
			{
				continue;
			}
			sscanf(szRead,"%s = %d\n",_szKey,iValue);
			bHaveFound = true;
			break;
		}
		return bHaveFound;
	}

	bool Read(char *szKey, int *iValue,char *szFileName)
	{
		assert(szKey);
		assert(iValue);
		assert(szFileName);
		bool bHaveFound = false;
		char szRead[E_MAX_LENGTH];
		char _szKey[E_MAX_LENGTH/2];
		const int iKEY_LEN = strlen(szKey);
		File::T_FILE * pFile;
		pFile = File::Open(szFileName, File::E_MODE_READ);
		while(iKEY_LEN<E_MAX_LENGTH && pFile!=NULL)
		{
			File::GetString(szRead,E_MAX_LENGTH,pFile);
			if (strcmp(szRead,"") == 0)
			{
				break;
			}
			if (strncmp(szKey,szRead,iKEY_LEN) != 0)
			{
				continue;
			}
			sscanf(szRead,"%s = %d\n",_szKey,iValue);
			bHaveFound = true;
			break;
		}
		return bHaveFound;
	}

	bool Read(char *szKey, char *szValue,char *szFileName)
	{
		assert(szKey);
		assert(szValue);
		assert(szFileName);
		bool bHaveFound = false;
		char szRead[E_MAX_LENGTH];
		char _szKey[E_MAX_LENGTH/2];
		const int iKEY_LEN = strlen(szKey);
		File::T_FILE * pFile;
		pFile = File::Open(szFileName, File::E_MODE_READ);
		while(iKEY_LEN<E_MAX_LENGTH && pFile!=NULL)
		{
			File::GetString(szRead,E_MAX_LENGTH,pFile);
			if (strcmp(szRead,"") == 0)
			{
				break;
			}
			if (strncmp(szKey,szRead,iKEY_LEN) != 0)
			{
				continue;
			}
			sscanf(szRead,"%s = %s\n",_szKey,szValue);
			bHaveFound = true;
			break;
		}
		return bHaveFound;
	}
};

M_END_NAMESPACE(mylib)

#endif//_MYLIB_INIFILE_H_INCLUDE_20090903_

